package com.wh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wh.dao.BaseDao;
import com.wh.dao.ext.SysUserRoleDao;
import com.wh.model.BaseModel;
import com.wh.model.SysUserRole;
import com.wh.service.IExt.ISysUserRoleService;
import com.legend.utils.*;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:2014-02-13 15:05:35
*/
public class SysUserRoleService extends BaseServiceImp implements ISysUserRoleService {

    Logger log = Logger.getRootLogger();

    private SysUserRoleService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static ISysUserRoleService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static ISysUserRoleService getInstance() {
        if (instance == null) {
            synchronized (ISysUserRoleService.class) {
                if (instance == null) {
                    instance = new SysUserRoleService();
                }
            }
        }
        return instance;
    }

    /**
     * 批量删除数据
     */
    public ReturnResult delByIds(String ids) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        try {
            SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
            result = delByMap(dao, ids);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * 分页数据查询
     *
     * @param pageIndex
     * @param pageSize
     * @param map
     * @return
     */
    public ReturnResult selectByMap(int pageIndex, int pageSize, Map<String, Object> map) {
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        int recordNum = (pageIndex - 1) * pageSize;
        List<SysUserRole> lst = session.selectList(ConfigUtils.getInstance().getDaoPackage(".SysUserRoleDao.selectByMap"), map, new RowBounds(recordNum, pageSize));
        int total = dao.count(map);
        ReturnResult result = new ReturnResult();
        result.setMsg(i18nUtils.getVal("100001000"));
        result.setRows(lst);
        result.setTotal(total);
        result.setStatus(Status.SUCCESS);
        session.close();
        return result;
    }

    @Override
    public ReturnResult selectByMap(Map<String, Object> map) {
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        List<SysUserRole> userRoles = dao.selectByMap(map);
        ReturnResult result = new ReturnResult();
        result.setRows(userRoles);
        result.setStatus(Status.SUCCESS);
        session.close();
        return result;
    }

    @Override
    public ReturnResult insertByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        ReturnResult result = insertByModel(dao, model);
        session.close();
        return result;
    }

    @Override
    public ReturnResult editByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        ReturnResult result = editByModel(dao, model);
        session.close();
        return result;
    }

    /**
     * 根据ID获取一条数据
     *
     * @param id
     * @return
     */
    public ReturnResult selectById(String id) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.ERROR);
        BaseDao dao = session.getMapper(SysUserRoleDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        List<SysUserRole> lst = dao.selectByMap(map);
        if (lst.size() == 1) {
            result.setData(lst.get(0));
            result.setStatus(Status.SUCCESS);
        } else {
            result.setMsg(i18nUtils.getVal("100004000"));
        }
        session.close();
        return result;
    }

    /**
     * 批量审核数据
     */
    public ReturnResult audit(int state, String[] idArr) {
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("idArr", idArr);
        map.put("state", state);
        Integer num = dao.audit(map);
        ReturnResult result = new ReturnResult();
        if (num > 0) {
            result.setMsg(i18nUtils.getVal("100001000"));
            result.setStatus(Status.SUCCESS);
        }
        session.close();
        return result;
    }

    /**
     * 排序
     *
     * @param sorts
     * @param ids
     * @return
     */
    public ReturnResult sort(String sorts, String ids) {
        ReturnResult result;
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        result = sort(session, dao, sorts, ids);
        if (session != null)
            session.close();
        return result;
    }

    /**
     * 添加数据
     */
    public ReturnResult Add(List<SysUserRole> lst, String userId) {
        ReturnResult result = new ReturnResult();
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        try {
            //SysUserRole delForUser = new SysUserRole();
            //delForUser.setUserId(userId);// 把当前用户的关联关系删除
            String[] idArr = new String[1];
            idArr[0] = userId.toString();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("idArr", idArr);
            int effect = dao.delByMap(map);
            for (SysUserRole entity : lst) {
                entity.setId(StringUtils.getGuid());
                dao.insertByModel(entity);
            }
            session.commit();
            result.setMsg(i18nUtils.getVal("100001000"));
            result.setStatus(Status.SUCCESS);
        } catch (Exception e) {
            session.rollback();
           // log.error(e.getMessage());
            result.setMsg(i18nUtils.getVal("100002000"));
            result.setStatus(Status.ERROR);
        } finally {
            session.close();
        }

        return result;
    }

    /**
     * 当前用户的拥有角色
     *
     * @param userId
     * @return
     */
    public ReturnResult currentUserSubRole(String userId) {
        SqlSession session = getSqlSession();
        SysUserRoleDao dao = session.getMapper(SysUserRoleDao.class);
        SysUserRole entity = new SysUserRole();
        entity.setUserId(userId);
        List<SysUserRole> lst = dao.selectByModel(entity);
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.SUCCESS);
        List<String> idlst = new ArrayList<String>();
        for (SysUserRole uRole : lst) {
            idlst.add(uRole.getRoleId());
        }
        result.setData(JSONObject.toJSONString(idlst));
        session.close();
        return result;
    }
}

